package atomic_value

import (
	"sync/atomic"
)

type Rectangle struct {
	length int
	width  int
}

var rect atomic.Value

var rectGlobal *Rectangle = new(Rectangle)

func UpdateWithAtomicValue(width, length int) {
	rectLocal := new(Rectangle)
	rectLocal.width = width
	rectLocal.length = length
	rect.Store(rectLocal)
}

func UpdateNormal(width, length int) {
	rectGlobal.width = width
	rectGlobal.length = length
}

//func main() {
//	wg := sync.WaitGroup{}
//	wg.Add(10)
//	// 10 个协程并发更新
//	for i := 0; i < 10; i++ {
//		go func() {
//			defer wg.Done()
//			UpdateWithAtomicValue(i, i+5)
//		}()
//	}
//	wg.Wait()
//	_r := rect.Load().(*Rectangle)
//	fmt.Printf("rect.width=%d\nrect.length=%d\n", _r.width, _r.length)
//}
